import 'dart:convert';

import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/services.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_yn_goods/config/app_config.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/address_service.dart';
import 'package:flutter_yn_goods/utils/http/http_export.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:flutter_yn_goods/utils/log/utils_log.dart';
import 'package:flutter_yn_goods/widgets/common_normal_dialog.dart';
import 'package:get/get.dart';

// import 'package:dvhcvn/dvhcvn.dart' as dvhcvn;
import 'package:url_launcher/url_launcher.dart';

class MineLogic extends GetxController {
  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  void loginOut() async {
    var toOutLogin = await CommonNormalDialog(
      title: LocaleKeys.logout.tr,
      message: LocaleKeys.logout_confirm.tr,
      barrierDismissible: false,
    ).show();
    if (true == toOutLogin) {
      // await HttpManager().post(API.loginOut, uiType: HttpUIType.loading);
      AppUserManager().logout();
      // if(response.isSuccess){
      //   AppUserManager().logout();
      // }else{
      //   LoadingUtils.showToast("退出失败：${response.codeAndMsg}");
      // }
    }
  }

  void cancelAccount() async {
    var toCancel = await CommonNormalDialog(
      title: LocaleKeys.cancel_account.tr,
      message: LocaleKeys.cancel_account_confirm.tr,
      barrierDismissible: false,
      onSure: () async {
        var response = await HttpManager()
            .get(API.cancelAccount, uiType: HttpUIType.loading);

        if (response.isSuccess) {
          LoadingUtils.showToast(LocaleKeys.cancel_account_success.tr);
          AppUserManager().logout();
        } else {
          LoadingUtils.showToast(
              '${LocaleKeys.cancel_account_failed.tr}${response.message}');
        }
      },
    ).show();

    // if (true == toCancel) {
    //   var response = await HttpManager().get(
    //     API.cancelAccount,
    //     uiType: HttpUIType.loading
    //   );
    //
    //   if (response.isSuccess) {
    //     LoadingUtils.showToast(LocaleKeys.cancel_account_success.tr);
    //     AppUserManager().logout();
    //   } else {
    //     LoadingUtils.showToast('${LocaleKeys.cancel_account_failed.tr}${response.message}');
    //   }
    // }
  }

  openEmailApp() async {
    final Uri emailLaunchUri = Uri(
      scheme: 'mailto',
      path: AppConfig.appConfig?.kefuMail, // 可以填一个默认邮箱地址
    );

    // if (await canLaunchUrl(emailLaunchUri)) {
    await launchUrl(emailLaunchUri);
    // } else {
    //   // 可选：提示用户未安装邮箱客户端
    //   print('无法打开邮箱应用');
    // }
  }
}
